<?php

/*
 * Copyright 2018 zhc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Get some statics data
 * The class can run standalone to save memory
 * @author Zhu Chen <zhuchen@greatqq.com>
 */
class libmisc {
    
    /**
     * Get the number of books the user has rent
     * @param int $uid The user ID
     * @return The count number
     */
    public function usercount(int $uid ) :int    {
        $rslt = DB::select('books', '`rent`='.$uid, '`id`', true, 'COUNT(*)');
        return $rslt['COUNT(*)'];
    }
    
    /**
     * Count the number of book the user had rent in the past
     * @param int $uid User ID
     * @return int The number of record in the history
     */
    public function historycount(int $uid) :int   {
        $rslt = DB::select('bookhistory', '`user`='.$uid.' AND `action` = '.Book::BOOK_ACTION_RENTOUT, '`id`', true, 'COUNT(*)');
        return $rslt['COUNT(*)'];
    }
    
    /**
     * Get the history list of a user
     * @param int $user the user id
     * @param int $page The page id
     * @param int $num Nr. of data in each query
     * @return type The array of user history
     */
    public function show_user_history(int $user,int $page=0,int $num=10){
        $st = $page*$num;
        return DB::select('bookhistory', '`user`='.$user,'`time` DESC LIMIT '.$st.','.$num);
    }
}
